package com.jjwww.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjwww.common.QueryPageParam;
import com.jjwww.common.Result;
import com.jjwww.entity.Menu;
import com.jjwww.entity.User;
import com.jjwww.service.impl.MenuServiceImpl;
import com.jjwww.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjwww
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private MenuServiceImpl menuService;

    @GetMapping("/list")
    public List<User> list(){
        return userService.list();

    }
    //查找
    @GetMapping("/findByUn")
    public Result findByUn(@RequestParam String username){
        List list = userService.lambdaQuery().eq(User::getUsername,username).list();
        return list.size()>0?Result.suc(list):Result.fail();
    }
    //编辑按钮中的更新
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.suc():Result.fail();
    }
    //编辑按钮中的删除
    @GetMapping ("/del")
    public Result del(@RequestParam String id){
        return userService.removeById(id)?Result.suc():Result.fail();
    }

    //注册功能
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        //检验用户名是否存在
        List list = userService.lambdaQuery()
                .eq(User::getUsername,user.getUsername()).list();

        if (list.size()>0){
            return Result.fail();
        } boolean saveSuccess = userService.save(user);
        if (saveSuccess){
            return Result.suc();
        }else {
            return Result.fail();
        }
    }


    //登录功能
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        List list = userService.lambdaQuery()
                .eq(User::getUsername,user.getUsername())
                .eq(User::getPassword,user.getPassword()).list();

        if(list.size()>0){
            User user1 = (User) list.get(0);
            List menuList = menuService.lambdaQuery().like(Menu::getMenuRight,user1.getIsadmin()).list();
            HashMap res = new HashMap<>();
            res.put("user",user1);
            res.put("menu",menuList);
            return Result.suc(res);
        }
        return Result.fail();
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        return userService.save(user)?Result.suc():Result.fail();
    }

    /**
     * 修改
     */
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return userService.removeById(id);
    }

    /**
     * 查询
     * @return
     */
    @PostMapping("/listP")
    public Result listp(@RequestBody User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(user.getName())){
            lambdaQueryWrapper.like(User::getName,user.getName());
        }
        return Result.suc(userService.list(lambdaQueryWrapper));
    }
    /**
     * 分页
     */
    //添加分页拦截器
    @PostMapping("/listPage")
//    1.public List<User> listp(@RequestBody HashMap map){
    public List<User> listp(@RequestBody QueryPageParam query){

        HashMap param = query.getParam();
        String name= (String)param.get("name");
        System.out.println("name==="+ (String)param.get("name"));


        ///System.out.println(query);

        //1.System.out.println("num===" + (String) map.get("pageSize"));

       /* System.out.println("num===" + query.getPageNum());
        System.out.println("size===" + query.getPageSize());

        HashMap param = query.getParam();
        System.out.println("id==="+ (String)param.get("id"));
        System.out.println("name==="+ (String)param.get("name"));*/
        /*LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getName,user.getName());
        return userService.list(lambdaQueryWrapper);*/


        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getName,name);


        IPage result = userService.page(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());
        return  result.getRecords();


    }
    //编写分页的mapper方法
    @PostMapping("/listPageC")
    public List<User> listPageC(@RequestBody QueryPageParam query){

        HashMap param = query.getParam();
        String name= (String)param.get("name");
        System.out.println("name==="+ (String)param.get("name"));

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.like(User::getName,name);

        IPage result = userService.pageC(page);
        System.out.println("total=="+result.getTotal());
        return  result.getRecords();
    }
    //自定义SQL使用wrapper
    @PostMapping("/listPageCC")
    public List<User> listPageCC(@RequestBody QueryPageParam query){

        HashMap param = query.getParam();
        String name= (String)param.get("name");
        System.out.println("name==="+ (String)param.get("name"));

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getUsername,name);

        IPage result = userService.pageCC(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());
        return  result.getRecords();
    }
    //后端返回数据封装
    @PostMapping("/listPageR")
    public Result listPageR(@RequestBody QueryPageParam query){

        HashMap param = query.getParam();
        String name= (String)param.get("name");
        String isadmin= (String)param.get("isadmin");
//        System.out.println("name==="+ (String)param.get("name"));

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(User::getName,name);
        }
        if (StringUtils.isNotBlank(isadmin)){
            lambdaQueryWrapper.eq(User::getIsadmin,isadmin);
        }


        IPage result = userService.pageCC(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());
        return Result.suc(result.getRecords(), result.getTotal());
    }
}
